package com.xiaodu.acer.phoneplay.di.module;

import android.app.Application;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.xiaodu.acer.phoneplay.App;

import java.lang.reflect.Type;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

/**
 * @author 小杜 QQ:1392229149
 * @name phone
 * @class name：com.xiaodu.acer.phoneplay.d2.moudle
 * @class describe
 * @time 2017/12/16 21:24
 * @change
 * @chang time
 * @class describe
 */
@Module
public class AppModule {

    private Application mApplication;

    public AppModule(App application) {
        this.mApplication = application;
    }

    @Provides
    @Singleton
    public Application provideApplication() {
        return mApplication;
    }

    @Provides
    @Singleton
    public Gson provideGson() {
        Gson gson = new GsonBuilder()
                .setDateFormat( "yyyy-MM-dd hh:mm:ss" )
                .registerTypeAdapter( Double.class, new JsonSerializer<Double>() {
                    @Override
                    public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) {
                        if (src == src.longValue())
                            return new JsonPrimitive( src.longValue() );
                        return new JsonPrimitive( src );
                    }
                } )
                .create();
        return gson;
    }


    // FIXME: 2018/1/19 App里的数据，抽时间看看能不能放在dagger2中
  /*  private static DaoSession daoSession;

    *//*
   * 配置数据库
   *//*
    private void setupDatabase() {
        //创建数据库shop.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "phone.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoInstant() {
        return daoSession;
    }*/


}
